/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.util.*;
import java.io.*;

/**
 *
 * @author phong
 */
public class Util {

    public static String[] tokenize(String s, String reg) {
        String[] tokens = s.split(reg);
        for (int i = 0; i < tokens.length; i++) {
            tokens[i] = tokens[i].trim();
        }
        return tokens;
    }
    
    public static String detokenize(String[] tokens, int begin, int len, String reg) {
        len = Math.min(len, tokens.length-begin);
        StringBuffer buff = new StringBuffer();
        buff.append(tokens[begin]);
        for (int i = 1; i < len; i++) {
            buff.append(reg).append(tokens[begin+i]);
        }
        return buff.toString();
    }
        
    public static void saveToFile(String filename, Object o) {
        try {
            FileOutputStream fout = new FileOutputStream(filename);
            ObjectOutputStream oos = new ObjectOutputStream(fout);
            oos.writeObject(o);
            oos.close();
        }
        catch (IOException e) {
            System.err.println(e);
        }
    }
    
    public static Object loadFromFile(String filename) {
        Object o = null;
        try {
            FileInputStream fin = new FileInputStream(filename);
            ObjectInputStream ois = new ObjectInputStream(fin);
            o = ois.readObject();
            ois.close();
        }
        catch (Exception e) {
            System.err.println(e);
            return null;
        }
        return o;
    }
        
    public static double[] createDoubleArray(int n, double value) {
        double[] a = new double[n];
        for (int i = 0; i < n; i++)
            a[i] = value;
        return a;
    }
    
    public static double[] getSubDoubleArray(double[] a, int start, int end) {
        double[] suba = new double[end-start+1];
        for (int i = start; i <= end; i++) {
            suba[i-start] = a[i];
        }
        return suba;
    }
            

    public static double takeElmWiseMulThenSum(double[] a1, double[] a2) {
        assert a1.length == a2.length;
        double sum = 0;
        for (int i = 0; i < a1.length; i++) 
            sum += a1[i] * a2[i];
        
        return sum;
    }
    
    public static double[] plus(double[] a1, double[] a2) {
        assert a1.length == a2.length;
        double[] a = new double[a1.length];
        for (int i = 0 ; i < a.length; i++)
            a[i] = a1[i] + a2[i];
        return a;
    }
    
    public static String doubleArrayToString(double[] a) {
        
        assert a != null && a.length > 0;
        
        StringBuffer buff = new StringBuffer();
        buff.append("[").append(a[0]);
        for (int i = 1; i < a.length; i++) {
            buff.append(",").append(a[i]);
        }
        buff.append("]");
        return buff.toString();
    }

    public static String joinToString(Collection a, String s) {
        if (a.isEmpty())
            return "";
        
        StringBuffer buff = new StringBuffer();
        Iterator itor = a.iterator();
        buff.append(itor.next());

        while (itor.hasNext()) {
            buff.append(s).append(itor.next());
        }

        return buff.toString();
    }
    
    public static Set<String> toStringSet(Collection c) {
        Set<String> s = new HashSet<String>();
        for (Object o : c) {
            s.add(o.toString());
        }
        return s;
    }

    public static void main(String[] args) {
        List<Integer> l = new LinkedList<Integer>();
        l.add(0);
        l.add(1);
        l.add(100);
        System.out.println(joinToString(l, "*"));
    }
}
